package com.bls.dao.impl;

import com.bls.dao.HistoryRecordDao;
import com.bls.entity.HistoryRecord;
import com.fy.toolhelper.db.BaseDaoImpl;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoliang
 * @date 2022/01/10 20:12
 **/
public class HistoryRecordDaoImpl extends BaseDaoImpl<HistoryRecord> implements HistoryRecordDao {
    public HistoryRecordDaoImpl() throws Exception {
    }

    @Override
    public HistoryRecord getLastHistoryRecord(Connection connection, Long analyseId) throws SQLException {

        String sql = "SELECT * FROM bls_history_record WHERE analyse_id=? ORDER BY start_time DESC LIMIT 0,1";

        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setLong(1, analyseId);
        ResultSet rs = pstm.executeQuery();
        HistoryRecord historyRecord = null;
        while (rs.next()) {
            historyRecord = getHistoryRecordEntity(rs);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return historyRecord;
    }

    @Override
    public List<HistoryRecord> pageConditionHistoryRecordList(Connection connection, HistoryRecord historyRecordQuery, Integer pageSize, Integer current) throws SQLException {

        int start = pageSize * (current - 1);
        StringBuilder sql = new StringBuilder("SELECT * FROM bls_history_record WHERE 1 = 1 ");

        if (!StringUtils.isEmpty(historyRecordQuery.getAnalyseId())) {
            sql.append(" and analyse_id = ").append(historyRecordQuery.getAnalyseId());
        }
        if (!StringUtils.isEmpty(historyRecordQuery.getBandId())) {
            sql.append(" and band_id = ").append(historyRecordQuery.getBandId());
        }
        if (!StringUtils.isEmpty(historyRecordQuery.getId())) {
            sql.append(" and id = ").append(historyRecordQuery.getId());
        }
        sql.append(" limit ? , ?");

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        List<HistoryRecord> historyRecordList = new ArrayList<>();
        while (rs.next()) {
            HistoryRecord historyRecordEntity = getHistoryRecordEntity(rs);
            historyRecordList.add(historyRecordEntity);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return historyRecordList;
    }

    @Override
    public int pageConditionHistoryRecordTotal(Connection connection, HistoryRecord historyRecordQuery) throws SQLException {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM bls_history_record WHERE 1 = 1 ");

        if (!StringUtils.isEmpty(historyRecordQuery.getAnalyseId())) {
            sql.append(" and analyse_id = ").append(historyRecordQuery.getAnalyseId());
        }
        if (!StringUtils.isEmpty(historyRecordQuery.getBandId())) {
            sql.append(" and band_id = ").append(historyRecordQuery.getBandId());
        }
        if (!StringUtils.isEmpty(historyRecordQuery.getId())) {
            sql.append(" and id = ").append(historyRecordQuery.getId());
        }

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        ResultSet rs = pstm.executeQuery();
        int count = -1;
        while (rs.next()) {
            count = rs.getInt("count");
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return count;
    }

    private HistoryRecord getHistoryRecordEntity(ResultSet rs) throws SQLException {
        HistoryRecord historyRecord = new HistoryRecord();
        historyRecord.setId(rs.getLong("id"));
        historyRecord.setAnalyseId(rs.getLong("analyse_id"));
        historyRecord.setBandId(rs.getLong("band_id"));
        historyRecord.setStartUserId(rs.getLong("start_user_id"));
        historyRecord.setStartUserName(rs.getString("start_user_name"));
        historyRecord.setStopUserId(rs.getLong("stop_user_id"));
        historyRecord.setStopUserName(rs.getString("stop_user_name"));
        historyRecord.setStartTime(rs.getTimestamp("start_time"));
        historyRecord.setEndTime(rs.getTimestamp("end_time"));
        return historyRecord;
    }
}
